home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / i386-tdep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-07  |  17.2 KB  |  681 lines

  1. /* Intel 386 target-dependent stuff.
  2.    Copyright (C) 1988, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "frame.h"
  22. #include "inferior.h"
  23. #include "gdbcore.h"
  24.  
  25. #ifdef USE_PROC_FS    /* Target dependent support for /proc */
  26. #include <sys/procfs.h>
  27. #endif
  28.  
  29. static long
  30. i386_get_frame_setup PARAMS ((int));
  31.  
  32. static void
  33. i386_follow_jump PARAMS ((void));
  34.  
  35. static void
  36. codestream_read PARAMS ((unsigned char *, int));
  37.  
  38. static void
  39. codestream_seek PARAMS ((int));
  40.  
  41. static unsigned char 
  42. codestream_fill PARAMS ((int));
  43.  
  44. /* helper functions for tm-i386.h */
  45.  
  46. /* Stdio style buffering was used to minimize calls to ptrace, but this
  47.    buffering did not take into account that the code section being accessed
  48.    may not be an even number of buffers long (even if the buffer is only
  49.    sizeof(int) long).  In cases where the code section size happened to
  50.    be a non-integral number of buffers long, attempting to read the last
  51.    buffer would fail.  Simply using target_read_memory and ignoring errors,
  52.    rather than read_memory, is not the correct solution, since legitimate
  53.    access errors would then be totally ignored.  To properly handle this
  54.    situation and continue to use buffering would require that this code
  55.    be able to determine the minimum code section size granularity (not the
  56.    alignment of the section itself, since the actual failing case that
  57.    pointed out this problem had a section alignment of 4 but was not a
  58.    multiple of 4 bytes long), on a target by target basis, and then
  59.    adjust it's buffer size accordingly.  This is messy, but potentially
  60.    feasible.  It probably needs the bfd library's help and support.  For
  61.    now, the buffer size is set to 1.  (FIXME -fnf) */
  62.  
  63. #define CODESTREAM_BUFSIZ 1    /* Was sizeof(int), see note above. */
  64. static CORE_ADDR codestream_next_addr;
  65. static CORE_ADDR codestream_addr;
  66. static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
  67. static int codestream_off;
  68. static int codestream_cnt;
  69.  
  70. #define codestream_tell() (codestream_addr + codestream_off)
  71. #define codestream_peek() (codestream_cnt == 0 ? \
  72.                codestream_fill(1): codestream_buf[codestream_off])
  73. #define codestream_get() (codestream_cnt-- == 0 ? \
  74.              codestream_fill(0) : codestream_buf[codestream_off++])
  75.  
  76. static unsigned char 
  77. codestream_fill (peek_flag)
  78.     int peek_flag;
  79. {
  80.   codestream_addr = codestream_next_addr;
  81.   codestream_next_addr += CODESTREAM_BUFSIZ;
  82.   codestream_off = 0;
  83.   codestream_cnt = CODESTREAM_BUFSIZ;
  84.   read_memory (codestream_addr,
  85.            (unsigned char *)codestream_buf,
  86.            CODESTREAM_BUFSIZ);
  87.   
  88.   if (peek_flag)
  89.     return (codestream_peek());
  90.   else
  91.     return (codestream_get());
  92. }
  93.  
  94. static void
  95. codestream_seek (place)
  96.     int place;
  97. {
  98.   codestream_next_addr = place / CODESTREAM_BUFSIZ;
  99.   codestream_next_addr *= CODESTREAM_BUFSIZ;
  100.   codestream_cnt = 0;
  101.   codestream_fill (1);
  102.   while (codestream_tell() != place)
  103.     codestream_get ();
  104. }
  105.  
  106. static void
  107. codestream_read (buf, count)
  108.      unsigned char *buf;
  109.      int count;
  110. {
  111.   unsigned char *p;
  112.   int i;
  113.   p = buf;
  114.   for (i = 0; i < count; i++)
  115.     *p++ = codestream_get ();
  116. }
  117.  
  118. /* next instruction is a jump, move to target */
  119.  
  120. static void
  121. i386_follow_jump ()
  122. {
  123.   int long_delta;
  124.   short short_delta;
  125.   char byte_delta;
  126.   int data16;
  127.   int pos;
  128.   
  129.   pos = codestream_tell ();
  130.   
  131.   data16 = 0;
  132.   if (codestream_peek () == 0x66)
  133.     {
  134.       codestream_get ();
  135.       data16 = 1;
  136.     }
  137.   
  138.   switch (codestream_get ())
  139.     {
  140.     case 0xe9:
  141.       /* relative jump: if data16 == 0, disp32, else disp16 */
  142.       if (data16)
  143.     {
  144.       codestream_read ((unsigned char *)&short_delta, 2);
  145.  
  146.       /* include size of jmp inst (including the 0x66 prefix).  */
  147.       pos += short_delta + 4; 
  148.     }
  149.       else
  150.     {
  151.       codestream_read ((unsigned char *)&long_delta, 4);
  152.       pos += long_delta + 5;
  153.     }
  154.       break;
  155.     case 0xeb:
  156.       /* relative jump, disp8 (ignore data16) */
  157.       codestream_read ((unsigned char *)&byte_delta, 1);
  158.       pos += byte_delta + 2;
  159.       break;
  160.     }
  161.   codestream_seek (pos);
  162. }
  163.  
  164. /*
  165.  * find & return amound a local space allocated, and advance codestream to
  166.  * first register push (if any)
  167.  *
  168.  * if entry sequence doesn't make sense, return -1, and leave 
  169.  * codestream pointer random
  170.  */
  171.  
  172. static long
  173. i386_get_frame_setup (pc)
  174.      int pc;
  175. {
  176.   unsigned char op;
  177.   
  178.   codestream_seek (pc);
  179.   
  180.   i386_follow_jump ();
  181.   
  182.   op = codestream_get ();
  183.   
  184.   if (op == 0x58)        /* popl %eax */
  185.     {
  186.       /*
  187.        * this function must start with
  188.        * 
  189.        *    popl %eax          0x58
  190.        *    xchgl %eax, (%esp)  0x87 0x04 0x24
  191.        * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
  192.        *
  193.        * (the system 5 compiler puts out the second xchg
  194.        * inst, and the assembler doesn't try to optimize it,
  195.        * so the 'sib' form gets generated)
  196.        * 
  197.        * this sequence is used to get the address of the return
  198.        * buffer for a function that returns a structure
  199.        */
  200.       int pos;
  201.       unsigned char buf[4];
  202.       static unsigned char proto1[3] = { 0x87,0x04,0x24 };
  203.       static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
  204.       pos = codestream_tell ();
  205.       codestream_read (buf, 4);
  206.       if (bcmp (buf, proto1, 3) == 0)
  207.     pos += 3;
  208.       else if (bcmp (buf, proto2, 4) == 0)
  209.     pos += 4;
  210.       
  211.       codestream_seek (pos);
  212.       op = codestream_get (); /* update next opcode */
  213.     }
  214.   
  215.   if (op == 0x55)        /* pushl %ebp */
  216.     {            
  217.       /* check for movl %esp, %ebp - can be written two ways */
  218.       switch (codestream_get ())
  219.     {
  220.     case 0x8b:
  221.       if (codestream_get () != 0xec)
  222.         return (-1);
  223.       break;
  224.     case 0x89:
  225.       if (codestream_get () != 0xe5)
  226.         return (-1);
  227.       break;
  228.     default:
  229.       return (-1);
  230.     }
  231.       /* check for stack adjustment 
  232.        *
  233.        *  subl $XXX, %esp
  234.        *
  235.        * note: you can't subtract a 16 bit immediate
  236.        * from a 32 bit reg, so we don't have to worry
  237.        * about a data16 prefix 
  238.        */
  239.       op = codestream_peek ();
  240.       if (op == 0x83)
  241.     {
  242.       /* subl with 8 bit immed */
  243.       codestream_get ();
  244.       if (codestream_get () != 0xec)
  245.         /* Some instruction starting with 0x83 other than subl.  */
  246.         {
  247.           codestream_seek (codestream_tell () - 2);
  248.           return 0;
  249.         }
  250.       /* subl with signed byte immediate 
  251.        * (though it wouldn't make sense to be negative)
  252.        */
  253.       return (codestream_get());
  254.     }
  255.       else if (op == 0x81)
  256.     {
  257.       /* subl with 32 bit immed */
  258.       int locals;
  259.       codestream_get();
  260.       if (codestream_get () != 0xec)
  261.         /* Some instruction starting with 0x81 other than subl.  */
  262.         {
  263.           codestream_seek (codestream_tell () - 2);
  264.           return 0;
  265.         }
  266.       /* subl with 32 bit immediate */
  267.       codestream_read ((unsigned char *)&locals, 4);
  268.       SWAP_TARGET_AND_HOST (&locals, 4);
  269.       return (locals);
  270.     }
  271.       else
  272.     {
  273.       return (0);
  274.     }
  275.     }
  276.   else if (op == 0xc8)
  277.     {
  278.       /* enter instruction: arg is 16 bit unsigned immed */
  279.       unsigned short slocals;
  280.       codestream_read ((unsigned char *)&slocals, 2);
  281.       SWAP_TARGET_AND_HOST (&slocals, 2);
  282.       codestream_get (); /* flush final byte of enter instruction */
  283.       return (slocals);
  284.     }
  285.   return (-1);
  286. }
  287.  
  288. /* Return number of args passed to a frame.
  289.    Can return -1, meaning no way to tell.  */
  290.  
  291. /* on the 386, the instruction following the call could be:
  292.  *  popl %ecx        -  one arg
  293.  *  addl $imm, %esp  -  imm/4 args; imm may be 8 or 32 bits
  294.  *  anything else    -  zero args
  295.  */
  296.  
  297. int
  298. i386_frame_num_args (fi)
  299.      struct frame_info *fi;
  300. {
  301.   int retpc;                        
  302.   unsigned char op;                    
  303.   struct frame_info *pfi;
  304.  
  305.   int frameless;
  306.  
  307.   FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
  308.   if (frameless)
  309.     /* In the absence of a frame pointer, GDB doesn't get correct values
  310.        for nameless arguments.  Return -1, so it doesn't print any
  311.        nameless arguments.  */
  312.     return -1;
  313.  
  314.   pfi = get_prev_frame_info (fi);            
  315.   if (pfi == 0)
  316.     {
  317.       /* Note:  this can happen if we are looking at the frame for
  318.      main, because FRAME_CHAIN_VALID won't let us go into
  319.      start.  If we have debugging symbols, that's not really
  320.      a big deal; it just means it will only show as many arguments
  321.      to main as are declared.  */
  322.       return -1;
  323.     }
  324.   else
  325.     {
  326.       retpc = pfi->pc;                    
  327.       op = read_memory_integer (retpc, 1);            
  328.       if (op == 0x59)                    
  329.     /* pop %ecx */                   
  330.     return 1;                
  331.       else if (op == 0x83)
  332.     {
  333.       op = read_memory_integer (retpc+1, 1);    
  334.       if (op == 0xc4)                
  335.         /* addl $<signed imm 8 bits>, %esp */    
  336.         return (read_memory_integer (retpc+2,1)&0xff)/4;
  337.       else
  338.         return 0;
  339.     }
  340.       else if (op == 0x81)
  341.     { /* add with 32 bit immediate */
  342.       op = read_memory_integer (retpc+1, 1);    
  343.       if (op == 0xc4)                
  344.         /* addl $<imm 32>, %esp */        
  345.         return read_memory_integer (retpc+2, 4) / 4;
  346.       else
  347.         return 0;
  348.     }
  349.       else
  350.     {
  351.       return 0;
  352.     }
  353.     }
  354. }
  355.  
  356. /*
  357.  * parse the first few instructions of the function to see
  358.  * what registers were stored.
  359.  *
  360.  * We handle these cases:
  361.  *
  362.  * The startup sequence can be at the start of the function,
  363.  * or the function can start with a branch to startup code at the end.
  364.  *
  365.  * %ebp can be set up with either the 'enter' instruction, or 
  366.  * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
  367.  * but was once used in the sys5 compiler)
  368.  *
  369.  * Local space is allocated just below the saved %ebp by either the
  370.  * 'enter' instruction, or by 'subl $<size>, %esp'.  'enter' has
  371.  * a 16 bit unsigned argument for space to allocate, and the
  372.  * 'addl' instruction could have either a signed byte, or
  373.  * 32 bit immediate.
  374.  *
  375.  * Next, the registers used by this function are pushed.  In
  376.  * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
  377.  * (and sometimes a harmless bug causes it to also save but not restore %eax);
  378.  * however, the code below is willing to see the pushes in any order,
  379.  * and will handle up to 8 of them.
  380.  *
  381.  * If the setup sequence is at the end of the function, then the
  382.  * next instruction will be a branch back to the start.
  383.  */
  384.  
  385. void
  386. i386_frame_find_saved_regs (fip, fsrp)
  387.      struct frame_info *fip;
  388.      struct frame_saved_regs *fsrp;
  389. {
  390.   long locals;
  391.   unsigned char *p;
  392.   unsigned char op;
  393.   CORE_ADDR dummy_bottom;
  394.   CORE_ADDR adr;
  395.   int i;
  396.   
  397.   bzero (fsrp, sizeof *fsrp);
  398.   
  399.   /* if frame is the end of a dummy, compute where the
  400.    * beginning would be
  401.    */
  402.   dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
  403.   
  404.   /* check if the PC is in the stack, in a dummy frame */
  405.   if (dummy_bottom <= fip->pc && fip->pc <= fip->frame) 
  406.     {
  407.       /* all regs were saved by push_call_dummy () */
  408.       adr = fip->frame;
  409.       for (i = 0; i < NUM_REGS; i++) 
  410.     {
  411.       adr -= REGISTER_RAW_SIZE (i);
  412.       fsrp->regs[i] = adr;
  413.     }
  414.       return;
  415.     }
  416.   
  417.   locals = i386_get_frame_setup (get_pc_function_start (fip->pc));
  418.   
  419.   if (locals >= 0) 
  420.     {
  421.       adr = fip->frame - 4 - locals;
  422.       for (i = 0; i < 8; i++) 
  423.     {
  424.       op = codestream_get ();
  425.       if (op < 0x50 || op > 0x57)
  426.         break;
  427.       fsrp->regs[op - 0x50] = adr;
  428.       adr -= 4;
  429.     }
  430.     }
  431.   
  432.   fsrp->regs[PC_REGNUM] = fip->frame + 4;
  433.   fsrp->regs[FP_REGNUM] = fip->frame;
  434. }
  435.  
  436. /* return pc of first real instruction */
  437.  
  438. int
  439. i386_skip_prologue (pc)
  440.      int pc;
  441. {
  442.   unsigned char op;
  443.   int i;
  444.   
  445.   if (i386_get_frame_setup (pc) < 0)
  446.     return (pc);
  447.   
  448.   /* found valid frame setup - codestream now points to 
  449.    * start of push instructions for saving registers
  450.    */
  451.   
  452.   /* skip over register saves */
  453.   for (i = 0; i < 8; i++)
  454.     {
  455.       op = codestream_peek ();
  456.       /* break if not pushl inst */
  457.       if (op < 0x50 || op > 0x57) 
  458.     break;
  459.       codestream_get ();
  460.     }
  461.   
  462.   i386_follow_jump ();
  463.   
  464.   return (codestream_tell ());
  465. }
  466.  
  467. void
  468. i386_push_dummy_frame ()
  469. {
  470.   CORE_ADDR sp = read_register (SP_REGNUM);
  471.   int regnum;
  472.   char regbuf[MAX_REGISTER_RAW_SIZE];
  473.   
  474.   sp = push_word (sp, read_register (PC_REGNUM));
  475.   sp = push_word (sp, read_register (FP_REGNUM));
  476.   write_register (FP_REGNUM, sp);
  477.   for (regnum = 0; regnum < NUM_REGS; regnum++)
  478.     {
  479.       read_register_gen (regnum, regbuf);
  480.       sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
  481.     }
  482.   write_register (SP_REGNUM, sp);
  483. }
  484.  
  485. void
  486. i386_pop_frame ()
  487. {
  488.   FRAME frame = get_current_frame ();
  489.   CORE_ADDR fp;
  490.   int regnum;
  491.   struct frame_saved_regs fsr;
  492.   struct frame_info *fi;
  493.   char regbuf[MAX_REGISTER_RAW_SIZE];
  494.   
  495.   fi = get_frame_info (frame);
  496.   fp = fi->frame;
  497.   get_frame_saved_regs (fi, &fsr);
  498.   for (regnum = 0; regnum < NUM_REGS; regnum++) 
  499.     {
  500.       CORE_ADDR adr;
  501.       adr = fsr.regs[regnum];
  502.       if (adr)
  503.     {
  504.       read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
  505.       write_register_bytes (REGISTER_BYTE (regnum), regbuf,
  506.                 REGISTER_RAW_SIZE (regnum));
  507.     }
  508.     }
  509.   write_register (FP_REGNUM, read_memory_integer (fp, 4));
  510.   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
  511.   write_register (SP_REGNUM, fp + 8);
  512.   flush_cached_frames ();
  513.   set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  514.                     read_pc ()));
  515. }
  516.  
  517. #ifdef USE_PROC_FS    /* Target dependent support for /proc */
  518.  
  519. /*  The /proc interface divides the target machine's register set up into
  520.     two different sets, the general register set (gregset) and the floating
  521.     point register set (fpregset).  For each set, there is an ioctl to get
  522.     the current register set and another ioctl to set the current values.
  523.  
  524.     The actual structure passed through the ioctl interface is, of course,
  525.     naturally machine dependent, and is different for each set of registers.
  526.     For the i386 for example, the general register set is typically defined
  527.     by:
  528.  
  529.     typedef int gregset_t[19];        (in <sys/regset.h>)
  530.  
  531.     #define GS    0            (in <sys/reg.h>)
  532.     #define FS    1
  533.     ...
  534.     #define UESP    17
  535.     #define SS    18
  536.  
  537.     and the floating point set by:
  538.  
  539.     typedef struct fpregset
  540.       {
  541.         union
  542.           {
  543.         struct fpchip_state    // fp extension state //
  544.         {
  545.           int state[27];    // 287/387 saved state //
  546.           int status;        // status word saved at exception //
  547.         } fpchip_state;
  548.         struct fp_emul_space    // for emulators //
  549.         {
  550.           char fp_emul[246];
  551.           char fp_epad[2];
  552.         } fp_emul_space;
  553.         int f_fpregs[62];    // union of the above //
  554.           } fp_reg_set;
  555.         long f_wregs[33];        // saved weitek state //
  556.     } fpregset_t;
  557.  
  558.     These routines provide the packing and unpacking of gregset_t and
  559.     fpregset_t formatted data.
  560.  
  561.  */
  562.  
  563. /* This is a duplicate of the table in i386-xdep.c. */
  564.  
  565. static int regmap[] = 
  566. {
  567.   EAX, ECX, EDX, EBX,
  568.   UESP, EBP, ESI, EDI,
  569.   EIP, EFL, CS, SS,
  570.   DS, ES, FS, GS,
  571. };
  572.  
  573.  
  574. /*  Given a pointer to a general register set in /proc format (gregset_t *),
  575.     unpack the register contents and supply them as gdb's idea of the current
  576.     register values. */
  577.  
  578. void
  579. supply_gregset (gregsetp)
  580.      gregset_t *gregsetp;
  581. {
  582.   register int regno;
  583.   register greg_t *regp = (greg_t *) gregsetp;
  584.   extern int regmap[];
  585.  
  586.   for (regno = 0 ; regno < NUM_REGS ; regno++)
  587.     {
  588.       supply_register (regno, (char *) (regp + regmap[regno]));
  589.     }
  590. }
  591.  
  592. void
  593. fill_gregset (gregsetp, regno)
  594.      gregset_t *gregsetp;
  595.      int regno;
  596. {
  597.   int regi;
  598.   register greg_t *regp = (greg_t *) gregsetp;
  599.   extern char registers[];
  600.   extern int regmap[];
  601.  
  602.   for (regi = 0 ; regi < NUM_REGS ; regi++)
  603.     {
  604.       if ((regno == -1) || (regno == regi))
  605.     {
  606.       *(regp + regmap[regno]) = *(int *) ®isters[REGISTER_BYTE (regi)];
  607.     }
  608.     }
  609. }
  610.  
  611. #if defined (FP0_REGNUM)
  612.  
  613. /*  Given a pointer to a floating point register set in /proc format
  614.     (fpregset_t *), unpack the register contents and supply them as gdb's
  615.     idea of the current floating point register values. */
  616.  
  617. void 
  618. supply_fpregset (fpregsetp)
  619.      fpregset_t *fpregsetp;
  620. {
  621.   register int regno;
  622.   
  623.   /* FIXME: see m68k-tdep.c for an example, for the m68k. */
  624. }
  625.  
  626. /*  Given a pointer to a floating point register set in /proc format
  627.     (fpregset_t *), update the register specified by REGNO from gdb's idea
  628.     of the current floating point register set.  If REGNO is -1, update
  629.     them all. */
  630.  
  631. void
  632. fill_fpregset (fpregsetp, regno)
  633.      fpregset_t *fpregsetp;
  634.      int regno;
  635. {
  636.   int regi;
  637.   char *to;
  638.   char *from;
  639.   extern char registers[];
  640.  
  641.   /* FIXME: see m68k-tdep.c for an example, for the m68k. */
  642. }
  643.  
  644. #endif    /* defined (FP0_REGNUM) */
  645.  
  646. #endif  /* USE_PROC_FS */
  647.  
  648. #ifdef GET_LONGJMP_TARGET
  649.  
  650. /* Figure out where the longjmp will land.  Slurp the args out of the stack.
  651.    We expect the first arg to be a pointer to the jmp_buf structure from which
  652.    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
  653.    This routine returns true on success. */
  654.  
  655. int
  656. get_longjmp_target(pc)
  657.      CORE_ADDR *pc;
  658. {
  659.   CORE_ADDR sp, jb_addr;
  660.  
  661.   sp = read_register(SP_REGNUM);
  662.  
  663.   if (target_read_memory(sp + SP_ARG0, /* Offset of first arg on stack */
  664.              &jb_addr,
  665.              sizeof(CORE_ADDR)))
  666.     return 0;
  667.  
  668.  
  669.   SWAP_TARGET_AND_HOST(&jb_addr, sizeof(CORE_ADDR));
  670.  
  671.   if (target_read_memory(jb_addr + JB_PC * JB_ELEMENT_SIZE, pc,
  672.              sizeof(CORE_ADDR)))
  673.     return 0;
  674.  
  675.   SWAP_TARGET_AND_HOST(pc, sizeof(CORE_ADDR));
  676.  
  677.   return 1;
  678. }
  679.  
  680. #endif /* GET_LONGJMP_TARGET */
  681.